package com.example.lbstest.controller;

import com.example.lbstest.impl.CardMapperServiceImpl;
import com.example.lbstest.impl.ClassMapperServiceImpl;
import com.example.lbstest.pojo.Card;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Controller
@RequestMapping("/card")
public class CardController {
    @Autowired
    CardMapperServiceImpl cardMapperService;

    @RequestMapping("/getCard")
    @ResponseBody
    public Card getCard(@RequestParam("class_number")String class_number,
                         @RequestParam("code")String code){
        System.out.println("-----获取card成功（单个）-----");
        return cardMapperService.getCard(class_number, code);
    }

    @RequestMapping("/punchCard")
    @ResponseBody
    public String punchCard(@RequestParam("student_number")String student_number,
                            @RequestParam("class_number")String class_number,
                            @RequestParam("code")String code,
                            @RequestParam("status")String status){

        int rr = 0;
        if(status.equals("出勤")){
            rr = cardMapperService.punchCardIn(code,class_number);
        }else if(status.equals("迟到")){
            rr = cardMapperService.punchCardDelay(code,class_number);
        }
        int rr1 = cardMapperService.insertPunchCard(student_number, class_number, code, getDate(), status);
        if(rr == 1 && rr1 == 1){
            System.out.println("打卡成功");
            return "success";
        }else{
            System.out.println("打卡失败");
            return "fail";
        }
    }


    @RequestMapping("/getCardList")
    @ResponseBody
    public List<Card> getCardList(@RequestParam("teacher_code")String teacher_code){
        System.out.println("-----获取card列表成功-----");
        return cardMapperService.getCardList(teacher_code);
    }

    @RequestMapping("/setStatus")
    @ResponseBody
    public String setStatus(@RequestParam("id")String id){
        int rr = cardMapperService.setStatus(id);
        if(rr == 1){
            System.out.println("-----本次考勤结束------");
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/insertOneCard")
    @ResponseBody
    public String insertOneCard(@RequestParam("time")String time,
                                @RequestParam("code")String code,
                                @RequestParam("all_number")String all_number,
                                @RequestParam("teacher_code")String teacher_code,
                                @RequestParam("class_number")String class_number,
                                @RequestParam("stay_s")String stay_s,
                                @RequestParam("set_meter")String set_meter,
                                @RequestParam("address")String address,
                                @RequestParam("lat")String lat,
                                @RequestParam("lon")String lon){
        Card card = new Card();
        card.setTime(time);
        card.setAddress(address);
        card.setAll_number(Integer.valueOf(all_number));
        card.setCode(code);
        card.setStatus("正在考勤");
        card.setTeacher_code(teacher_code);
        card.setSet_meter(Integer.valueOf(set_meter));
        card.setId(getUId());
        card.setIn_number(0);
        card.setOut_number(0);
        card.setAsk_number(0);
        card.setDelay_number(0);
        card.setStay_s(Integer.valueOf(stay_s));
        card.setClass_number(class_number);
        card.setLat(Double.valueOf(lat));
        card.setLon(Double.valueOf(lon));

        //20min后的延迟操作
        delayTime(code,class_number);
        String id = card.getId();
        int rr = cardMapperService.insertOneCard(card);
        if(rr == 1){
            System.out.println("------card插入成功-----");
            return id;
        }
        return "fail";
    }
    //修改请假状态
    @RequestMapping("/changeAsk")
    @ResponseBody
    public String changeAsk(@RequestParam("student_number")String student_number,
                            @RequestParam("class_number")String class_number,
                            @RequestParam("code")String code){
        int rr = cardMapperService.updateOneCardWhere(student_number, class_number, code);
        int kk;
        if(rr == 0){
            cardMapperService.insertPunchCardAndAsk(class_number,student_number,code,getDate());
            kk = cardMapperService.punchCardAskOne(class_number, code);
        }else{
            kk = cardMapperService.punchCardAskTwo(class_number, code);
        }
        if(kk == 1){
            return "success";
        }
        return "fail";

    }

    private String getUId() {
        long time = System.currentTimeMillis();
        String s = String.valueOf(time);
        s=s.substring(3,13);
        long id = Long.valueOf(s);
        System.out.println(s.length());
        return String.valueOf(id);
    }

    //获取当前时间
    private String getDate() {
        Date dNow = new Date( );
        SimpleDateFormat ft =
                new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return ft.format(dNow);

    }

    //延时任务,刷新旷课人数，插入表单
    private void delayTime(String code,String class_number){
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // task to run goes here
                // 执行的输出的内容
                System.out.println("延迟操作已进行");
                int rr = cardMapperService.delayTimeUpdate(code,class_number);
                int rr1 = cardMapperService.delayTimeInsert(code,class_number,getDate());
                if(rr !=0 || rr1 != 0){
                    System.out.println("旷课人数记录成功");
                }
            }
        };
        Timer timer = new Timer();
        // 定义开始等待时间  --- 等待 20 min
        // 1000ms = 1s
        long delay = 1000 * 20 * 60;
        // schedules the task to be run in an interval
        // 安排任务在一段时间内运行
        timer.schedule(task,delay);
        //timer.scheduleAtFixedRate(task, delay, intevalPeriod);
    }
}
